/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2021年8月17日
 */
package com.massyframework.beanskin.maven.plugin;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.artifact.handler.ArtifactHandler;

/**
 * 支持加密的工件
 * @author huangkh
 *
 */
public class EncryptableArtifact extends DefaultArtifact {
	
	private List<String> classNames;
	private List<String> fileNames;
	
	protected EncryptableArtifact(Builder builder) {
		super(builder.groupId, builder.artifactId, builder.version, "compile", "jar", null, builder.artifactHandler);
		this.classNames = builder.classNames == null? 
				Collections.emptyList() :
					Collections.unmodifiableList(builder.classNames);
		this.fileNames = builder.fileNames == null ?
				Collections.emptyList() :
					Collections.unmodifiableList(builder.fileNames);
	}

	/**
	 * 要加密的类名
	 * @return {@link List}
	 */
	public List<String> getClassNames(){
		return this.classNames;
	}
	
	/**
	 * 要加密的文件名
	 * @return {@link List}
	 */
	public List<String> getFileNames(){
		return this.fileNames;
	}
	
	/**
	 * 是否有加密要求
	 * <p>
	 * 有加密的类或者加密文件，则返回{@code true}
	 * @return {@link boolean},返回{@code true}表示有，否则返回{@code false}
	 */
	public boolean hasEncrpyRequest() {
		return !this.classNames.isEmpty() || !this.fileNames.isEmpty();
	}
	
	/**
	 * 创建构建器实例
	 * @return {@link Builder}
	 */
	protected static Builder newBuilder() {
		return new Builder();
	}
	
	/**
	 * 构建器
	 */
	public static class Builder implements ArtifactBuilder {
		private String groupId;
		private String artifactId;
		private String version;
		private ArtifactHandler artifactHandler;
		private List<String> classNames;
		private List<String> fileNames;
		
		@Override
		public Builder groupId(String value) {
			this.groupId = value;
			return this;
		}

		@Override
		public Builder artifactId(String value) {
			this.artifactId = value;
			return this;
		}

		@Override
		public Builder version(String value) {
			this.version = value;
			return this;
		}
		
		public Builder artifactHandler(ArtifactHandler value) {
			this.artifactHandler = value;
			return this;
		}
		
		public Builder addClassName(String value) {
			if (value != null) {
				if (this.classNames == null ) {
					this.classNames = new ArrayList<>();
				}
				this.classNames.add(value);
			}
			
			return this;
		}
		
		public Builder addFileName(String value) {
			if (value != null) {
				if (this.fileNames == null) {
					this.fileNames = new ArrayList<>();
				}
				
				if (value.startsWith("/")) {
					value = value.substring(1);
				}
				this.fileNames.add(value);
			}
			return this;
		}
		
		public EncryptableArtifact build() {
			return new EncryptableArtifact(this);
		}
	}
}
